//
//  PersonDetailInfoViewController.m
//  MobileOffice
//
//  Created by jjkj on 16/12/9.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PersonDetailInfoViewController.h"
#import "PersonDetailInfoModel.h"
#import "PersonDetailInfoTableViewCell.h"
#import "PersonDetail2TableViewCell.h"

#define getCheckingInList @"getCheckingInList"//考勤信息查询
#define getAskForLeaveList @"getAskForLeaveList"//请假信息查询

@interface PersonDetailInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;//列表
}
@property (nonatomic,strong) PersonDetailInfoModel *infoModel;
@end

@implementation PersonDetailInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    //如果是请假公出考勤
    if (self.businessCode&&self.businessCode.length>0) {
        [self getAskForLeaveListRequest];
    }else{
        [self getCheckingInListRequest];
    }
    // Do any additional setup after loading the view.
}

#pragma mark - creatTableView
- (void)creatView{
    //self.view.backgroundColor=COLOR_BACKGROUND_GRAY;
    //标题
    UILabel *titleLabel=[MyControl creatLabelWithFrame:CGRectMake(0,20, IPHONE_WIDTH, 40) text:[NSString stringWithFormat:@"%@%@信息",self.empName,self.itemStr]];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.font=FONT_17;
    [self.view addSubview:titleLabel];
    
    UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 59, IPHONE_WIDTH, 1)];
    lineLabel.backgroundColor=BACKCOLOR_GRAY;
    [self.view addSubview:lineLabel];
    
    //列表
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, IPHONE_WIDTH, IPHONE_HEIGHT-60-40) style:UITableViewStylePlain];
    //_myTableView.backgroundColor=[UIColor redColor];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.tag=110;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.bounces=NO;
    //[self addTableViewUpdateRefresh:_tableView BindingIdentity:@""];
    //    [self addTableViewLoadMoreData:_tableView BindingIdentity:@""];
    [self.view addSubview:_tableView];
    
    //    self.definesPresentationContext = YES;
    UILabel *lineLabel2=[[UILabel alloc]initWithFrame:CGRectMake(0, IPHONE_HEIGHT-40, IPHONE_WIDTH, 1)];
    lineLabel2.backgroundColor=BACKCOLOR_GRAY;
    [self.view addSubview:lineLabel2];
    //返回按钮
    UIButton *backButton=[MyControl creatButtonWithFrame:CGRectMake(0, IPHONE_HEIGHT-39, IPHONE_WIDTH, 39) target:self sel:@selector(backClick) tag:120 image:nil title:@"关闭"];
    [self.view addSubview:backButton];
}



#pragma mark - TableViewDelagate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoModel.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //如果是请假公出考勤
    if (self.businessCode&&self.businessCode.length>0) {
        NSString *cellID=@"PersonDetail2TableViewCell";
        PersonDetail2TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell=[[PersonDetail2TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        //取出model  填充数据
        PersonDetailInfoModel *model=self.infoModel.dataArr[indexPath.row];
        cell.myTitleLabel.text=model.title;
        cell.infoLabel.text=model.reason;
        cell.timeLabel.text=[NSString stringWithFormat:@"%@至%@",model.beginTime,model.endTime];
        return cell;
    }else{
        NSString *cellID=@"SignInHistoryTableViewCell";
        PersonDetailInfoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell=[[PersonDetailInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        //取出model  填充数据
        PersonDetailInfoModel *model=self.infoModel.dataArr[indexPath.row];
        cell.myTitleLabel.text=model.empName;
        cell.infoLabel.text=model.time;
        cell.timeLabel.text=[NSString stringWithFormat:@"%@%@",self.itemStr,model.content1];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果是请假公出考勤
    if (self.businessCode&&self.businessCode.length>0) {
        return 56;
    }else{
        return 44;
    }
}


#pragma mark -考勤信息查询
- (void)getCheckingInListRequest{
    NSString *khswitchId=nil;
    if ([self.itemStr isEqualToString:@"迟到"]) {
        khswitchId=@"1";
    }else if ([self.itemStr isEqualToString:@"早退"]){
        khswitchId=@"2";
    }else if ([self.itemStr isEqualToString:@"旷工"]){
        khswitchId=@"3";
    }else{
        khswitchId=@"4";
    }
    NSDictionary *dict=@{
                         @"beginTime":self.beginStr?self.beginStr:@"",
                         @"endTime":self.endStr?self.endStr:@"",
                         @"empId":self.empIds?self.empIds:[LZXHelper getSessionID],
                         @"empCode":self.itemCode?self.itemCode:@"",
                         @"khswitchId":khswitchId
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    // __weak AddressBookViewController *mySelf=self;
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:[[UIApplication sharedApplication].delegate window]];
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getCheckingInList soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
        
        //解析数据
        self.infoModel=[[PersonDetailInfoModel alloc]initWithResponse:responseObject];
        if ([self.infoModel.code isEqualToString:@"200"]) {
            if (self.infoModel.dataArr.count==0) {
                //增加提示语
                [self addReminderViewWithFrame:_tableView.frame msg:@"暂时没有要查询的信息~" backView:_tableView];
            }else{
                //移除提示语
                [self removeReminderView];
            }
            [_tableView reloadData];
        }else if([self.infoModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:self.infoModel.msg View:[[UIApplication sharedApplication].delegate window]];
        }
    } failure:^(NSError *error) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
    }];
}

#pragma mark -请假信息查询
- (void)getAskForLeaveListRequest{
    NSDictionary *dict=@{
                         @"beginTime":self.beginStr?self.beginStr:@"",
                         @"endTime":self.endStr?self.endStr:@"",
                         @"empId":self.empIds?self.empIds:[LZXHelper getSessionID],
                         @"empCode":self.itemCode?self.itemCode:@"",
                         @"businessCode":self.businessCode?self.businessCode:@""
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    // __weak AddressBookViewController *mySelf=self;
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:[[UIApplication sharedApplication].delegate window]];
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getAskForLeaveList soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
        
        //解析数据
        self.infoModel=[[PersonDetailInfoModel alloc]initWithResponse:responseObject];
        if ([self.infoModel.code isEqualToString:@"200"]) {
            //            //如果有先移除
            //            if (!self.backScrollView) {
            //                //页面汇总信息按钮
            //                [self creatFunctionButtonView];
            //            }else{
            //                [self refreshInfoButton];
            //            }
            //
            if (self.infoModel.dataArr.count==0) {
                //增加提示语
                [self addReminderViewWithFrame:_tableView.frame msg:@"暂时没有要查询的信息~" backView:_tableView];
            }else{
                //移除提示语
                [self removeReminderView];
            }
            [_tableView reloadData];
        }else if([self.infoModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:self.infoModel.msg View:[[UIApplication sharedApplication].delegate window]];
        }
    } failure:^(NSError *error) {
        [LZXHelper hideLoadViewWithView:[[UIApplication sharedApplication].delegate window]];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
